<template>
  <div style="width: 100%" class="input-modal">
    <a-input
      v-model="name"
      placeholder="请选择"
      allow-clear
      readonly
      style="width: 100%"
      @clear.stop="handerClear"
    >
      <template #append>
        <div class="apppend-box" @click="select"><icon-more /></div>
      </template>
    </a-input>
    <userModal
      v-if="visible"
      :user="dept"
      :width="'1000'"
      :type="'dept'"
      :title="props.title"
      @close="close"
      @handle-ok="handleOk"
    />
  </div>
</template>

<!--新script-->
<script lang="ts" setup>
  import { ref, watch } from 'vue';
  import userModal from '@/common/components/userAndDept/userModal.vue';

  const props = defineProps({
    modelValue: {
      type: String,
      default: '',
    },
    /**
     * @param {string}
     * 回显值
     */
    labelValue: {
      type: String,
      default: '',
    },
    name: {
      type: String,
      default: '',
    },
    title: {
      type: String,
      default: '选择部门',
    },
  });
  const emit = defineEmits(['update:modelValue', 'update:name', 'change']);
  const visible = ref(false);
  const dept = ref({
    id: '',
    name: '',
  });
  const name = ref(props.labelValue);
  watch(
    () => props.labelValue,
    (n: string) => {
      if (n) {
        name.value = n;
      }
    },
    { deep: true }
  );
  watch(
    () => props.modelValue,
    (n: string) => {
      if (!n) {
        name.value = n;
      }
    },
    { deep: true }
  );
  const select = () => {
    visible.value = true;
  };
  const close = () => {
    visible.value = false;
  };
  const handleOk = (val: any) => {
    name.value = val.name;
    emit('update:modelValue', val.id);
    emit('update:name', val.name);
    emit('change', val);
  };
  const handerClear = () => {
    emit('update:modelValue', undefined);
    emit('change', undefined);
    emit('update:name', undefined);
  };
</script>

<style lang="less" scoped>
  .base-component {
    color: red;
  }
  .apppend-box {
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
</style>
